package com.alibaba.cloud.medicine.controller;

import com.alibaba.cloud.medicine.dto.MedItemDTO;
import com.alibaba.cloud.medicine.entity.MedItem;
import com.alibaba.cloud.medicine.service.MedItemService;
import com.alibaba.framework.domain.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/meditem")
public class MedItemController {

    @Autowired
    private MedItemService medItemService;

    @GetMapping("/list")
    public ResponseWrapper<List<MedItem>> getAll() {

        List<MedItem> medicineList = medItemService.getAll();
        return ResponseWrapper.Success(medicineList);
    }

    @PostMapping("/add")
    public ResponseWrapper add(@RequestBody MedItemDTO medItemDTO) {

        medItemService.add(medItemDTO);
        return ResponseWrapper.Success();
    }

    @PostMapping("/deduct")
    public ResponseWrapper<Integer> deduct(@RequestParam("medCode") String medCode, @RequestParam("medStock") Integer medStock) {

        int i = medItemService.deductStock(medCode, medStock);
        return ResponseWrapper.Success(i);
    }
}
